package com.lucenly.card.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lucenly.card.R;
import com.lucenly.card.adapter.CardAdapter;
import com.lucenly.card.adapter.MsgAdapter;
import com.lucenly.card.adapter.WelfareAdapter;
import com.lucenly.card.base.BaseFragment;
import com.lucenly.card.bean.MsgBean;
import com.lucenly.card.utils.DensityUtil;
import com.lucenly.card.utils.ScalePagerTransformer;
import com.lucenly.card.view.RecyclerItemDecoration;
import com.lucenly.card.view.viewpager.LoopViewPager;
import com.necer.calendar.Miui9Calendar;
import com.necer.entity.NDate;
import com.necer.listener.OnCalendarChangedListener;
import com.necer.utils.Attrs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class HomeNewFragment extends BaseFragment {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.rl_right)
    RelativeLayout rl_right;
    @BindView(R.id.miui9Calendar)
    Miui9Calendar miui9Calendar;
    //https://github.com/yannecer/NCalendar
    @BindView(R.id.tv_show)
    ImageView tv_show;
    @BindView(R.id.ll_show)
    LinearLayout ll_show;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_top_month)
    LinearLayout ll_top_month;
    LoopViewPager viewPager;
    CardAdapter adapter;
    List<MsgBean> list = new ArrayList<>();
    private List<Integer> mList = new ArrayList<>();
    WelfareAdapter vp_adapter;
    @Override
    protected int getLayoutID() {
        return R.layout.fragment_home_new;
    }

    @Override
    protected void initView(View contentView) {
        tv_title.setText(getResources().getString(R.string.app_name));
        ll_back.setVisibility(View.GONE);
        rl_right.setVisibility(View.VISIBLE);
        List<String> pointList = Arrays.asList("2018-12-01", "2018-12-02", "2018-12-3", "2018-12-23", "2019-01-05");
        miui9Calendar.setPointList(pointList);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        adapter = new CardAdapter();
        MsgBean msgBean = new MsgBean();
        msgBean.typeId = 1;
        msgBean.title = "测试";
        msgBean.content = "内容";
        msgBean.createTime = System.currentTimeMillis()/1000;
        list.add(msgBean);
        list.add(msgBean);
        list.add(msgBean);
        list.add(msgBean);
        list.add(msgBean);

        mList.add(R.color.red_btn_bg_color);
        mList.add(R.mipmap.banner_bg2x);
        mList.add(R.color.blue_btn_bg_pressed_color);

        View head = getLayoutInflater().inflate(R.layout.home_head,null);
        viewPager = head.findViewById(R.id.viewPager);
        viewPager.setPageTransformer(true, new ScalePagerTransformer());
        //设置Pager之间的间距
        viewPager.setPageMargin(-DensityUtil.dp2px(10));
        vp_adapter = new WelfareAdapter(mContext);
        vp_adapter.setDatas(mList);
        viewPager.setAdapter(vp_adapter);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setCurrentItem(vp_adapter.getCount() > 0 ? 1 : 0, true);

        adapter.addHeaderView(head);
        adapter.replaceData(list);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void bindEvent() {
        C(ll_show);
        miui9Calendar.setOnCalendarChangedListener(new OnCalendarChangedListener() {
            @Override
            public void onCalendarDateChanged(NDate date) {
                //日历回调 NDate包含公历、农历、节气、节假日、闰年等信息
            }

            @Override
            public void onCalendarStateChanged(boolean isMonthSate) {
                //日历状态回调， 月->周 isMonthSate返回false ，反之返回true
                if(isMonthSate){
                    tv_show.setImageResource(R.mipmap.ic_up);
                    ll_top_month.setVisibility(View.VISIBLE);
                }else{
                    tv_show.setImageResource(R.mipmap.ic_down);
                    ll_top_month.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    protected void processClick(View view) {
        switch (view.getId()){
            case R.id.ll_show:
                if(miui9Calendar.getSTATE()==Attrs.WEEK){
                    miui9Calendar.toMonth();
                }else{
                    miui9Calendar.toWeek();
                }
                break;
        }
    }
}
